from ensurepip import bootstrap
from django import forms
from django.forms import widgets
from app01 import models
from django.core.validators import RegexValidator
from utils import utils


class BootStripForm(object):
    bootstrap_class_exlude = ['remember', "item_image"]

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for name, field in self.fields.items():
            if name not in self.bootstrap_class_exlude:
                old_class = field.widget.attrs.get("class", "")

                field.widget.attrs['class'] = f"{old_class} form-control"
                field.widget.attrs['placeholder'] = f"请输入{field.label}"


class ManagerListForm(BootStripForm, forms.ModelForm):
    user_name = forms.CharField(label="用户名", validators=[RegexValidator(r'[a-zA-Z0-9]', '用户名只能包含数字或字母！')], min_length=5)
    pwd = forms.CharField(label="密码", widget=forms.PasswordInput(render_value=True), min_length=4,
                          validators=[RegexValidator(r'[a-zA-Z0-9,.]', '密码只能包含数字或字母！')])
    remember = forms.CharField(label="记住用户名", widget=forms.CheckboxInput(attrs={"class": ""}), required=False)

    class Meta:
        model = models.ManagerList
        fields = ["user_name", "pwd", "remember"]

    def clean_pwd(self):
        pwd = self.cleaned_data.get("pwd")
        # return pwd
        return utils.md5(pwd)


class EditUserForm(BootStripForm, forms.ModelForm):
    user_name = forms.CharField(label="用户名", validators=[RegexValidator(r'[a-zA-Z0-9]', '用户名只能包含数字或字母！')], min_length=5)
    pwd = forms.CharField(label="原密码", widget=forms.PasswordInput(render_value=True), min_length=4,
                          validators=[RegexValidator(r'[a-zA-Z0-9,.]', '密码只能包含数字或字母！')])
    new_pwd = forms.CharField(label="新密码", widget=forms.PasswordInput(render_value=True), min_length=4,
                              validators=[RegexValidator(r'[a-zA-Z0-9,.]', '密码只能包含数字或字母！')])
    conf_pwd = forms.CharField(label="确认新密码", widget=forms.PasswordInput(render_value=True), min_length=4,
                               validators=[RegexValidator(r'[a-zA-Z0-9,.]', '密码只能包含数字或字母！')])

    class Meta:
        model = models.ManagerList
        fields = ["user_name", "pwd", "new_pwd", "conf_pwd", ]

    def clean_pwd(self):
        pwd = self.cleaned_data.get("pwd")
        return utils.md5(pwd)


class EditNickname(BootStripForm, forms.ModelForm):
    nick_name = forms.CharField(label="新昵称", min_length=2)

    class Meta:
        model = models.ManagerList
        fields = ["nick_name", ]


class CustomerForm(BootStripForm, forms.ModelForm):
    phone = forms.CharField(label="手机号", validators=[RegexValidator(r'^(1[3|4|5|6|7|8|9])\d{9}$', '手机号格式错误！'), ])
    ill_date_period = forms.CharField(label="病史", validators=[
        RegexValidator(r'(^\d+年\d+月$)|(^\d+年$)|(^\d+月$)', '格式示例：3年或2月或4年1月'), ])

    class Meta:
        model = models.CustomerInfo
        exclude = ['pid', 'returning_visits_date', 'last_test_date']


class TestForm(BootStripForm, forms.ModelForm):
    class Meta:
        model = models.TestList
        fields = ["test_date", "trail_or_bought", "symptom", "test_treatment", "satisfaction", "feedback", "pid"
                  ]


class BuyingForm(BootStripForm, forms.ModelForm):
    class Meta:
        model = models.BuyingList
        fields = "__all__"


class BuyingEditForm(BootStripForm, forms.ModelForm):
    class Meta:
        model = models.BuyingList
        fields = ["bill_status", "operator"]

class TestImgForm(BootStripForm, forms.ModelForm):
    test_id = forms.IntegerField(label="检查id")
    reporters = [(0, "---------"), ]
    try:
        reporters.extend(models.ManagerList.objects.values_list("id", "user_name"))

    except Exception as E:
        pass

    test_reporter = forms.IntegerField(label="报告人", widget=widgets.Select(choices=reporters, ))

    class Meta:
        model = models.TestList
        fields = ["symptom", "test_result", "trail_or_bought", "test_treatment", "satisfaction", "feedback",
                  "test_reporter", "test_id",
                  "pid", "tid"]


class ManagerForm(BootStripForm, forms.ModelForm):
    class Meta:
        model = models.ManagerList
        fields = "__all__"

    def clean_pwd(self):
        pwd = self.cleaned_data.get("pwd")
        return utils.md5(pwd)


class ManagerEditForm(BootStripForm, forms.ModelForm):
    class Meta:
        model = models.ManagerList
        fields = "__all__"


class ItemPriceListForm(BootStripForm, forms.ModelForm):
    class Meta:
        model = models.ItemPriceList
        fields = "__all__"


class ReturningVisitsForm(BootStripForm, forms.ModelForm):

    class Meta:
        model = models.ReturningVisits
        fields = "__all__"
